<?php

namespace App\Admin\Controllers\Mp;

use App\Admin\Actions\Extensions\Mp\MpMaterialButton;
use App\Admin\Actions\Extensions\Mp\MpMessageButton;
use App\Admin\Renderable\Mp\MpMessageRender;
use App\Admin\Repositories\Mp\WxMpMessageReply;
use App\Models\Mp\WxMpMessage;
use App\Wen\Utils\AdminUtils;
use Dcat\Admin\Actions\Action;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxMpMessageReplyController extends AdminController
{
    protected $type_options = [
        1 => '全匹配',
        2 => '输入词包含触发词',
        3 => '触发词包含输入词'
    ];

    protected $type_labels = [
        1 => 'green',
        2 => 'yellow',
        3 => 'orange'
    ];
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxMpMessageReply(), function (Grid $grid) {
            $grid->model()->orderBy('order', 'desc')->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('word');
            $grid->column('type')->using($this->type_options)->label($this->type_labels);
            $grid->column('mp_message_id');
            $grid->column('order')->editable()->sortable();
            $grid->column('status')->switch();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });

            $grid->tools([
                new MpMessageButton(),
                new MpMaterialButton()
            ]);


        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxMpMessageReply(), function (Show $show) {
            $show->field('id');
            $show->field('word');
            $show->field('type');
            $show->field('mp_message_id');
            $show->field('order');
            $show->field('status');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxMpMessageReply(), function (Form $form) {
            $form->display('id');
            $form->html(function (){
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">特殊触发词：<br/>event_subscribe: 关注后触发</div>';
            }, '');
            $form->textarea('word')->required();
            $form->radio('type')->options($this->type_options)->default(1);

            $form->multipleSelectTable('mp_message_id')
                ->title('圈子列表')
                ->max(2)
                ->from(MpMessageRender::make())
                ->model(WxMpMessage::class, 'id', 'id', 'selectTable:WxMpMessageReplyController:mp_message_id')
                ->help('仅能选择一个，<a href="'.admin_url('mp_message').'" target="_blank">管理消息库 >> </a>')->required()->saving(function ($mp_message_id) {
                    if(is_array($mp_message_id)){
                        $mp_message_id = implode(',', $mp_message_id);
                    }
                    $mp_message_id = AdminUtils::_multipleSelectTable_order_process('selectTable:formPageMineTrait:app_sign_day_task', $mp_message_id);
                    if(strpos($mp_message_id, ',') !== false){
                        $mp_message_id = str_replace(['0,', '0, '], '', $mp_message_id);
                        $mp_message_id_arr = explode(',', $mp_message_id);
                        if(count($mp_message_id_arr) > 0 && $mp_message_id_arr[0] > 0){
                            $mp_message_id = $mp_message_id_arr[0];
                        }
                    }
                    return (int)($mp_message_id);
                });

            $form->number('order');
            $form->switch('status')->default(1);

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
